package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.behavior;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.bpmn.behavior.ExclusiveGatewayActivityBehavior;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Iterator;
import java.util.Map;

/**
 * 自定义排他网关行为类
 * @author zjh
 * @since 20170606
 */
public class CustomerExclusiveGatewayActivityBehavior extends ExclusiveGatewayActivityBehavior {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static Logger log = LoggerFactory.getLogger(ExclusiveGatewayActivityBehavior.class);

    @Override
    protected void leave(ActivityExecution execution) {
        if(log.isDebugEnabled()) {
            log.debug("Leaving activity \'{}\'", execution.getActivity().getId());
        }

        execution.getProcessBusinessKey();

        PvmTransition outgoingSeqFlow = null;
        String defaultSequenceFlow = (String)execution.getActivity().getProperty("default");
        Iterator transitionIterator = execution.getActivity().getOutgoingTransitions().iterator();

        Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("", "");


        PvmTransition defaultTransition;
        while(outgoingSeqFlow == null && transitionIterator.hasNext()) {

            defaultTransition = (PvmTransition)transitionIterator.next();

           /* defaultTransition = (PvmTransition)transitionIterator.next();
            Expression skipExpression = defaultTransition.getSkipExpression();
            if(SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression)) {
                if(SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression)) {
                    outgoingSeqFlow = defaultTransition;
                }
            } else {
                Condition condition = (Condition)defaultTransition.getProperty("condition");
                if(condition == null && (defaultSequenceFlow == null || !defaultSequenceFlow.equals(defaultTransition.getId())) || condition != null && condition.evaluate(defaultTransition.getId(), execution)) {
                    if(log.isDebugEnabled()) {
                        log.debug("Sequence flow \'{}\'selected as outgoing sequence flow.", defaultTransition.getId());
                    }

                    outgoingSeqFlow = defaultTransition;
                }
            }*/
        }

        if(outgoingSeqFlow != null) {
            execution.take(outgoingSeqFlow);
        } else {
            if(defaultSequenceFlow == null) {
                throw new ActivitiException("No outgoing sequence flow of the exclusive gateway \'" + execution.getActivity().getId() + "\' could be selected for continuing the process");
            }

            defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
            if(defaultTransition == null) {
                throw new ActivitiException("Default sequence flow \'" + defaultSequenceFlow + "\' not found");
            }

            execution.take(defaultTransition);
        }

    }
}
